package org.softmed.rest.admin.config;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import main.RESTEditor;

import org.softmed.rest.admin.DetailChooser;
import org.softmed.rest.admin.DetailLevelListener;
import org.softmed.rest.admin.DetailChooser.DetailLevel;
import org.softmed.rest.admin.interfaces.CloseListener;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.PathChangedListener;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;
import org.softmed.swing.IconManager;

public class ConfigurationActionPanel extends JPanel implements ActionListener,
		PathChangedListener, SelectionPathChanged, DocumentListener {

	EventListener addResourceListener;

	int width = 20;
	int height = 20;

	JLabel validateError = new JLabel(IconManager.warning); // OK
	JLabel changed = new JLabel(IconManager.changed); // OK

	JButton create = new JButton(IconManager.newDocument); // OK
	JButton retrieve = new JButton(IconManager.leftArrow); // OK
	JButton save = new JButton(IconManager.save); // OK
	JButton refresh = new JButton(IconManager.refresh);

	JButton add = new JButton("Add Module", IconManager.add); // OK
	JButton remove = new JButton("Remove All", IconManager.remove); // OK

	DetailChooser detailChooser = new DetailChooser(true);

	JButton saveConfigurationFileOnly = new JButton(
			"Save Configuration file Only");

	FileHandler fileHandler;

	private boolean active;

	private RESTEditor editor;

	private ConfigurationPathHandler path;

	public ConfigurationActionPanel(ConfigurationPathHandler path,
			RESTEditor editor) {
		this.editor = editor;
		this.path = path;
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		// pathField.getDocument().addDocumentListener(this);

		setPreferredSize(new Dimension(970, 30));
		setMaximumSize(new Dimension(970, 30));
		setMinimumSize(new Dimension(970, 30));

		// pathField.setPreferredSize(new Dimension(240, 25));
		// pathField.setMaximumSize(new Dimension(240, 25));
		// pathField.setMinimumSize(new Dimension(240, 25));

		// add(pathField);
		addButton(create);
		addButton(retrieve);
		addButton(save);
		addButton(refresh);
		addButton(saveConfigurationFileOnly);

		addButton(add);
		addButton(remove);

		add(detailChooser);

		resizeButton(saveConfigurationFileOnly, 190, height);

		add(Box.createHorizontalGlue());

		add(validateError);
		validateError.setVisible(false);

		create
				.setToolTipText("Create new XML file on the file browser's currently selected directory");
		retrieve
				.setToolTipText("Get the XML file path from the browser's currently selected XML file");
		save.setToolTipText("Saves current module configuration XML file");
		validateError.setToolTipText("There was an error validating the file.");
		changed
				.setToolTipText("The configuration has changed from the currently stored version.");

		deactivate();

	}

	protected void addButton(JButton button) {
		resizeButton(button, width, height);
		button.addActionListener(this);
		button.setEnabled(false);
		add(button);
	}

	protected void resizeButton(JButton button, int width, int height) {
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setMinimumSize(new Dimension(width, height));
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();

		if (source == retrieve) {
			String path = fileHandler.retrieveFilePath();
			this.path.setText(path);
		} else if (source == save) {
			editor.saveAllConfiguration(path.getText());
		} else if (source == refresh) {
			String text = path.getText();
			// editor.close();
			// editor.setConfigurationMode();
			// pathField.setText(text);
			editor.fileOpened(text);
			// todo load
		} else if (source == create) {
			String path = editor.createFileWithPopup();
			this.path.setText(path);
		} else if (source == saveConfigurationFileOnly) {
			editor.saveConfigurationOnly(path.getText());
		} else if (source == add) {
//			editor.addNewModule();
		} else if (source == remove) {
			editor.removeAllModules();
		}

	}

	public EventListener getAddResourceListener() {
		return addResourceListener;
	}

	public void setAddResourceListener(EventListener addResourceListener) {
		this.addResourceListener = addResourceListener;
	}

	public void selectionPathChanged(boolean directory, String name,
			String parent) {
		retrieve.setEnabled(false);
		create.setEnabled(false);

		if (name == null) {

		} else if (name.endsWith(".xml")) {
			retrieve.setEnabled(true);
		}
		create.setEnabled(true);

	}

	public FileHandler getFileHandler() {
		return fileHandler;
	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;
	}

	@Override
	public void pathChanged(String path) {
		save.setEnabled(false);
		refresh.setEnabled(false);

		if (path.endsWith(".xml")) {
			save.setEnabled(true);
			refresh.setEnabled(true);
		}

	}

	private void updateOnPathChange() {
		if (!active)
			return;

		save.setEnabled(false);
		refresh.setEnabled(false);

		String path = this.path.getText();

		if (path == null) {

		} else if (path.endsWith(".xml") && path.startsWith("/")
				&& path.length() > 5) {
			save.setEnabled(true);
			refresh.setEnabled(true);
		}
	}

	@Override
	public void changedUpdate(DocumentEvent arg0) {

		updateOnPathChange();
	}

	@Override
	public void insertUpdate(DocumentEvent arg0) {
		updateOnPathChange();

	}

	@Override
	public void removeUpdate(DocumentEvent arg0) {
		updateOnPathChange();

	}

	public void activate() {
		setActive(true);
	}

	public void deactivate() {
		setActive(false);
	}

	public void setActive(boolean active) {
		this.active = active;
		create.setEnabled(active);
		retrieve.setEnabled(active);
		save.setEnabled(active);
		refresh.setEnabled(active);
		saveConfigurationFileOnly.setEnabled(active);

		detailChooser.setEnabled(active);
	}

	public void setDetailListener(DetailLevelListener listener) {
		detailChooser.setDetailListener(listener);
	}

	public void setDetailLevel(DetailLevel detailLevel) {
		detailChooser.setDetailLevel(detailLevel);
	}

	public DetailLevel getDetailLevel() {
		return detailChooser.getDetailLevel();
	}

}
